
#include "../include/DcPacketManager.h"

#include "../../../include/DCData.h"

#include "../../../include/json.hpp"
#include "../../../src/common/include/Base64.hpp"
#include "../../../src/common/include/common.h"
#include "../../../src/logger/include/CLogger.h"

using json = nlohmann::json;


#define JSON_RETURN_ARRAY_EMPTY(list_obj) do{if(list_obj.empty()){ \
        j = json::array(); \
        return; \
    } }while(0)

template<typename T>
void json_to_obj(const json& j, std::string field, T& obj){
    if (field.empty()){
        return;
    }
    if (j.contains(field)){
        j.at(field).get_to(obj);
    }
}

void to_json(json& j, const ST_MODEL_INFO& stModelInfo)
{
    j = json{
        {"model", stModelInfo.model},
        {"port",stModelInfo.port },
        {"addr",stModelInfo.addr},
        {"desc",stModelInfo.desc},
        {"manuID",stModelInfo.manuID},
        {"isreport",stModelInfo.isreport},
        {"nodeID",stModelInfo.nodeID},
        {"productID",stModelInfo.productID}
    };
}

void from_json(const json& j, ST_MODEL_INFO& stModelInfo)
{
    j.at("model").get_to(stModelInfo.model);
    j.at("port").get_to(stModelInfo.port);
    j.at("addr").get_to(stModelInfo.addr);
    j.at("desc").get_to(stModelInfo.desc);
    j.at("manuID").get_to(stModelInfo.manuID);
    j.at("isreport").get_to(stModelInfo.isreport);
    j.at("nodeID").get_to(stModelInfo.nodeID);
    j.at("productID").get_to(stModelInfo.productID);
}

void to_json(json& j, const std::list<ST_MODEL_INFO>& lstModelInfo)
{
    JSON_RETURN_ARRAY_EMPTY(lstModelInfo);
    for (auto itInfo = lstModelInfo.begin(); itInfo != lstModelInfo.end();itInfo++)
    {
        j.push_back(*itInfo);
    }
}

void from_json(const json& j, std::list<ST_MODEL_INFO>& lstModelInfo)
{
    for (auto js = j.begin(); js != j.end();js++)
    {
        ST_MODEL_INFO stModelInfo = *js;
        lstModelInfo.push_back(stModelInfo);
    }
}

void  to_json(json& j, const ST_MODEL_QUERY& stModelQuery)
{
    j = json{
        { "token", stModelQuery.token },
        { "timestamp", stModelQuery.timestamp },
        { "body", stModelQuery.lstModelNames }
    };
}

void  from_json(const json& j, ST_MODEL_QUERY& stModelQuery)
{
    json_to_obj<std::string>(j, "token", stModelQuery.token);
    json_to_obj<std::string>(j, "timestamp", stModelQuery.timestamp);
    json_to_obj<std::list<std::string>>(j, "body", stModelQuery.lstModelNames);
}

void to_json(json& j, const ST_DEV_REGISTER& stDevRegister)
{
    j = json{
        { "token", stDevRegister.token },
        { "timestamp", stDevRegister.timestamp },
        { "body", stDevRegister.lstModelInfo }
    };
}

void from_json(const json& j, ST_DEV_REGISTER& stDevRegister)
{
    json_to_obj<std::string>(j, "token", stDevRegister.token);
    json_to_obj<std::string>(j, "timestamp", stDevRegister.timestamp);
    json_to_obj<std::list<ST_MODEL_INFO>>(j, "body", stDevRegister.lstModelInfo);
}

void to_json(json& j, const ST_REGISTER_NOTIFYPT& stDevRegNotifyPt)
{
    j = json{
        { "model", stDevRegNotifyPt.model },
        { "port", stDevRegNotifyPt.port },
        { "addr", stDevRegNotifyPt.addr },
        { "desc", stDevRegNotifyPt.desc },
        { "guid",stDevRegNotifyPt.guid},
        { "dev",stDevRegNotifyPt.dev}
    };
}

void from_json(const json& j, ST_REGISTER_NOTIFYPT& stDevRegNotifyPt)
{
    json_to_obj<std::string>(j, "model", stDevRegNotifyPt.model);
    json_to_obj<std::string>(j, "port", stDevRegNotifyPt.port);
    json_to_obj<std::string>(j, "addr", stDevRegNotifyPt.addr);
    json_to_obj<std::string>(j, "desc", stDevRegNotifyPt.desc);
    json_to_obj<std::string>(j, "guid", stDevRegNotifyPt.guid);
    json_to_obj<std::string>(j, "dev", stDevRegNotifyPt.dev);
}

void to_json(json& j, const std::list<ST_REGISTER_NOTIFYPT>& lstRegNotifyPt)
{
    JSON_RETURN_ARRAY_EMPTY(lstRegNotifyPt);
    for (auto itRegPt = lstRegNotifyPt.begin(); itRegPt != lstRegNotifyPt.end();itRegPt++)
    {
        j.push_back(*itRegPt);
    }
}

void from_json(const json& j, std::list<ST_REGISTER_NOTIFYPT>& lstRegNotifyPt)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_REGISTER_NOTIFYPT stRegNotifyPt = *js;
        lstRegNotifyPt.push_back(stRegNotifyPt);
    }
}

void to_json(json& j, const ST_DEV_REGISTER_NOTIFY&  stDevRegNotify)
{
    j = json{
        { "token", stDevRegNotify.token },
        { "timestamp", stDevRegNotify.timestamp },
        { "body", stDevRegNotify.lstRegisterPts }
    };
}

void from_json(const json& j, ST_DEV_REGISTER_NOTIFY&  stDevRegNotify)
{
    json_to_obj<std::string>(j, "token", stDevRegNotify.token);
    json_to_obj<std::string>(j, "timestamp", stDevRegNotify.timestamp);
    json_to_obj<std::list<ST_REGISTER_NOTIFYPT>>(j, "body", stDevRegNotify.lstRegisterPts);
}

void to_json(json& j, const ST_DATACENTER_RSP& stDatacenterRsp)
{
    j = json{
        { "token", stDatacenterRsp.token },
        { "timestamp", stDatacenterRsp.timestamp },
        { "status", stDatacenterRsp.status }
    };
}

void from_json(const json& j, ST_DATACENTER_RSP& stDatacenterRsp)
{
    json_to_obj<std::string>(j, "token", stDatacenterRsp.token);
    json_to_obj<std::string>(j, "timestamp", stDatacenterRsp.timestamp);
    json_to_obj<std::string>(j, "status", stDatacenterRsp.status);

}

void to_json(json& j, const ST_MODEL_PTINFO& stModelPtInfo)
{
    j = json{
        { "name", stModelPtInfo.name },
        { "type", stModelPtInfo.type },
        { "unit", stModelPtInfo.unit },
        {"deadzone",stModelPtInfo.deadzone},
        {"ratio",stModelPtInfo.ratio},
        {"isreport",stModelPtInfo.isreport},
        {"userdefine",stModelPtInfo.userdefine}
    };
}

void from_json(const json& j, ST_MODEL_PTINFO& stModelPtInfo)
{
    json_to_obj<std::string>(j, "name", stModelPtInfo.name);
    json_to_obj<std::string>(j, "type", stModelPtInfo.type);
    json_to_obj<std::string>(j, "unit", stModelPtInfo.unit);
    json_to_obj<std::string>(j, "deadzone", stModelPtInfo.deadzone);
    json_to_obj<std::string>(j, "ratio", stModelPtInfo.ratio);
    json_to_obj<std::string>(j, "isreport", stModelPtInfo.isreport);
    json_to_obj<std::string>(j, "userdefine", stModelPtInfo.userdefine);
  
}

void to_json(json& j, const std::list<ST_MODEL_PTINFO>& lstModelPtInfo)
{
    JSON_RETURN_ARRAY_EMPTY(lstModelPtInfo);
    for (auto itModelPt = lstModelPtInfo.begin(); itModelPt != lstModelPtInfo.end(); itModelPt++)
    {
        j.push_back(*itModelPt);
    }
}

void from_json(const json& j, std::list<ST_MODEL_PTINFO>& lstModelPtInfo)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_MODEL_PTINFO stModelPt = *js;
        lstModelPtInfo.push_back(stModelPt);
    }
}

void to_json(json& j, const ST_MODEL_REGISTER& stModelRegister)
{
    j = json{
        { "token", stModelRegister.token },
        { "timestamp", stModelRegister.timestamp },
        { "model", stModelRegister.model},
        { "body", stModelRegister.lstModelInfo }
    };
}

void to_json(json& j, const std::list<std::string>& lstModelName)
{
    JSON_RETURN_ARRAY_EMPTY(lstModelName);
    for (auto itModel = lstModelName.begin(); itModel != lstModelName.end();itModel++)
    {
        j.push_back(*itModel);
    }
}

void from_json(const json& j, std::list<std::string>& lstModelName)
{
    for (auto js = j.begin(); js != j.end();js++)
    {
        std::string strName = *js;
        lstModelName.push_back(strName);
    }
}

void from_json(const json& j, ST_MODEL_REGISTER&  stModelRegister)
{
    json_to_obj<std::string>(j, "token", stModelRegister.token);
    json_to_obj<std::string>(j, "timestamp", stModelRegister.timestamp);
    json_to_obj<std::string>(j, "model", stModelRegister.model);
    json_to_obj<std::list<ST_MODEL_PTINFO>>(j, "body", stModelRegister.lstModelInfo);
}

void to_json(json& j, const ST_MODEL_UNREGISTER& stModelUnregister)
{
    j = json{
        { "token", stModelUnregister.token },
        { "timestamp", stModelUnregister.timestamp },
        { "body", stModelUnregister.lstmodels }
    };
}

void  to_json(json& j, const ST_REPORT_DATAPT& stReportDataPt)
{
    j = json{
        {"name",stReportDataPt.name},
        {"val",stReportDataPt.val},
        {"quality",stReportDataPt.quality},
        { "timestamp", stReportDataPt.timestamp }
    };
}

void from_json(const json& j, ST_REPORT_DATAPT& stReportDataPt)
{
    json_to_obj<std::string>(j, "name", stReportDataPt.name);
    json_to_obj<std::string>(j, "val", stReportDataPt.val);
    json_to_obj<std::string>(j, "quality", stReportDataPt.quality);
    json_to_obj<std::string>(j, "timestamp", stReportDataPt.timestamp);
}
void  to_json(json& j, const std::list<ST_REPORT_DATAPT>& lstReportPts)
{
    JSON_RETURN_ARRAY_EMPTY(lstReportPts);
    for (auto itRpt = lstReportPts.begin(); itRpt != lstReportPts.end(); itRpt++)
    {
        j.push_back(*itRpt);
    }
}

void from_json(const json& j, std::list<ST_REPORT_DATAPT>& lstReportPts)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_REPORT_DATAPT stReportPt = *js;
        lstReportPts.push_back(stReportPt);
    }
}

void to_json(json& j, const ST_DATACENTER_DATAREPORT& stDataCenterReport)
{
    j = json{
        { "token", stDataCenterReport.token },
        { "timestamp", stDataCenterReport.timestamp },
        {"data_row",stDataCenterReport.data_row},
        { "body", stDataCenterReport.lstReportPts }
    };
}

void from_json(const json& j, ST_DATACENTER_DATAREPORT& stDataCenterReport)
{
    json_to_obj<std::string>(j, "token", stDataCenterReport.token);
    json_to_obj<std::string>(j, "timestamp", stDataCenterReport.timestamp);
    json_to_obj<std::string>(j, "data_row", stDataCenterReport.data_row);
    json_to_obj<std::list<ST_REPORT_DATAPT>>(j, "body", stDataCenterReport.lstReportPts);
}

void to_json(json& j, const ST_UNREG_DEVINFO& stDevInfo)
{
    j = json{
        { "model", stDevInfo.model },
        { "port", stDevInfo.port },
        { "addr", stDevInfo.addr },
        { "desc", stDevInfo.desc }
    };
}

void from_json(const json& j, ST_UNREG_DEVINFO& stDevInfo)
{
    j.at("model").get_to(stDevInfo.model);
    j.at("port").get_to(stDevInfo.port);
    j.at("addr").get_to(stDevInfo.addr);
    j.at("desc").get_to(stDevInfo.desc);
}

void to_json(json& j, const std::list<ST_UNREG_DEVINFO>& lstModelInfo)
{
    JSON_RETURN_ARRAY_EMPTY(lstModelInfo);
    for (auto itInfo = lstModelInfo.begin(); itInfo != lstModelInfo.end(); itInfo++)
    {
        j.push_back(*itInfo);
    }
}

void from_json(const json& j, std::list<ST_UNREG_DEVINFO>& lstModelInfo)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_UNREG_DEVINFO stModelInfo = *js;
        lstModelInfo.push_back(stModelInfo);
    }
}
void to_json(json& j, const ST_DEV_UNREGISTER& stDevRegister)
{
    j = json{
        { "token", stDevRegister.token },
        { "timestamp", stDevRegister.timestamp },
        { "body", stDevRegister.lstDevInfo }
    };
}

void from_json(const json& j, ST_DEV_UNREGISTER& stDevRegister)
{
    json_to_obj<std::string>(j, "token", stDevRegister.token);
    json_to_obj<std::string>(j, "timestamp", stDevRegister.timestamp);
    json_to_obj<std::list<ST_UNREG_DEVINFO>>(j, "body", stDevRegister.lstDevInfo);
}

void to_json(json& j, const ST_SAME_MODEL_DEV_REGINFO& stSameModelDevInfo)
{
    j = json{
        {"guid",stSameModelDevInfo.guid},
        {"dev",stSameModelDevInfo.dev},
        {"addr",stSameModelDevInfo.addr},
        {"desc",stSameModelDevInfo.desc},
        {"isreport",stSameModelDevInfo.isreport},
        {"manuID",stSameModelDevInfo.manuID},
        {"nodeID",stSameModelDevInfo.nodeID},
        {"productID",stSameModelDevInfo.productID}
    };
}

void from_json(const json& j, ST_SAME_MODEL_DEV_REGINFO& stSameModelDevInfo)
{
    json_to_obj<std::string>(j, "guid", stSameModelDevInfo.guid);
    json_to_obj<std::string>(j, "dev", stSameModelDevInfo.dev);
    json_to_obj<std::string>(j, "addr", stSameModelDevInfo.addr);
    json_to_obj<std::string>(j, "desc", stSameModelDevInfo.desc);
    json_to_obj<std::string>(j, "isreport", stSameModelDevInfo.isreport);
    json_to_obj<std::string>(j, "manuID", stSameModelDevInfo.manuID);
    json_to_obj<std::string>(j, "nodeID", stSameModelDevInfo.nodeID);
    json_to_obj<std::string>(j, "productID", stSameModelDevInfo.productID);
}

void to_json(json& j, const std::list<ST_SAME_MODEL_DEV_REGINFO>& lstSameModelDevInfo)
{
    JSON_RETURN_ARRAY_EMPTY(lstSameModelDevInfo);
    for (auto itmd = lstSameModelDevInfo.begin(); itmd != lstSameModelDevInfo.end(); itmd++)
    {
        j.push_back(*itmd);
    }
}

void from_json(const json& j, std::list<ST_SAME_MODEL_DEV_REGINFO>& lstSameModelDevInfo)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_SAME_MODEL_DEV_REGINFO  stSameModelDev = *js;
        lstSameModelDevInfo.push_back(stSameModelDev);
    }
}

void to_json(json& j, const ST_REGISTER_MODEL_INFO& stRegisterModels)
{
    j = json{
        { "model", stRegisterModels.model },
        { "port",  stRegisterModels.port },
        { "body",  stRegisterModels.lstRegModels}
    };
}

void from_json(const json& j, ST_REGISTER_MODEL_INFO& stRegisterModels)
{
    json_to_obj<std::string>(j, "model", stRegisterModels.model);
    json_to_obj<std::string>(j, "port", stRegisterModels.port);
    json_to_obj<std::list<ST_SAME_MODEL_DEV_REGINFO>>(j, "body", stRegisterModels.lstRegModels);
}

void to_json(json& j, const std::list<ST_REGISTER_MODEL_INFO>&  lstRegisterModels)
{
    JSON_RETURN_ARRAY_EMPTY(lstRegisterModels);
    for (auto itreg = lstRegisterModels.begin(); itreg != lstRegisterModels.end(); itreg++)
    {
        j.push_back(*itreg);
    }
}

void from_json(const json& j, std::list<ST_REGISTER_MODEL_INFO>&  lstRegisterModels)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_REGISTER_MODEL_INFO stRegiterModel = *js;
        lstRegisterModels.push_back(stRegiterModel);
    }
}

void to_json(json& j, const ST_DEV_REGISTER_RELATIONSHIP& stDevRegisterRelation)
{
    j = json{
        { "token", stDevRegisterRelation.token },
        { "timestamp", stDevRegisterRelation.timestamp },
        { "body", stDevRegisterRelation.lstRegisterModels }
    };
}

void from_json(const json& j, ST_DEV_REGISTER_RELATIONSHIP& stDevRegisterRelation)
{
    json_to_obj<std::string>(j, "token", stDevRegisterRelation.token);
    json_to_obj<std::string>(j, "timestamp", stDevRegisterRelation.timestamp);
    json_to_obj<std::list<ST_REGISTER_MODEL_INFO>>(j, "body", stDevRegisterRelation.lstRegisterModels);
}

void to_json(json& j, const ST_SINGLE_MODEL_INFO& stSingleModelPts)
{
    j = json{
        { "model", stSingleModelPts.model },
        { "body", stSingleModelPts.lstModelptInfo }
    };
}

void from_json(const json& j, ST_SINGLE_MODEL_INFO& stSingleModelPts)
{
    json_to_obj<std::string>(j, "model", stSingleModelPts.model);
    json_to_obj<std::list<ST_MODEL_PTINFO>>(j, "body", stSingleModelPts.lstModelptInfo);
}

void to_json(json& j, const std::list<ST_SINGLE_MODEL_INFO>&  lstModels)
{
    JSON_RETURN_ARRAY_EMPTY(lstModels);
    for (auto itm= lstModels.begin(); itm != lstModels.end(); itm++)
    {
        j.push_back(*itm);
    }
}

void from_json(const json& j, std::list<ST_SINGLE_MODEL_INFO>&  lstModels)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_SINGLE_MODEL_INFO stSingleModel = *js;
        lstModels.push_back(stSingleModel);
    }
}

void to_json(json& j, const ST_MODEL_QUERY_RSP& stModelQueryRsp)
{
    j = json{
        { "token", stModelQueryRsp.token },
        { "timestamp", stModelQueryRsp.timestamp },
        { "body",stModelQueryRsp.lstModels }
    };
}
void from_json(const json& j, ST_MODEL_QUERY_RSP& stModelQueryRsp)
{
    json_to_obj<std::string>(j, "token", stModelQueryRsp.token);
    json_to_obj<std::string>(j, "timestamp", stModelQueryRsp.timestamp);
    json_to_obj<std::list<ST_SINGLE_MODEL_INFO>>(j, "body", stModelQueryRsp.lstModels);
}

void to_json(json& j, const ST_CHANGE_REPORT_DATAPT& stChangeReportPt)
{
    j = json{
        { "name", stChangeReportPt.name },
        { "id", stChangeReportPt.id },
        { "val", stChangeReportPt.val },
        { "unit", stChangeReportPt.unit },
        { "quality", stChangeReportPt.quality },
        { "timestamp", stChangeReportPt.timestamp }
    };
}

void from_json(const json& j, ST_CHANGE_REPORT_DATAPT& stChangeReportPt)
{
    json_to_obj<std::string>(j, "name", stChangeReportPt.name);
    json_to_obj<std::string>(j, "id", stChangeReportPt.id);
    json_to_obj<std::string>(j, "val", stChangeReportPt.val);
    json_to_obj<std::string>(j, "unit", stChangeReportPt.unit);
    json_to_obj<std::string>(j, "quality", stChangeReportPt.quality);
    json_to_obj<std::string>(j, "timestamp", stChangeReportPt.timestamp);
}

void to_json(json& j, const std::list<ST_CHANGE_REPORT_DATAPT>& lstReportPts)
{
    JSON_RETURN_ARRAY_EMPTY(lstReportPts);
    for (auto itPt = lstReportPts.begin(); itPt != lstReportPts.end(); itPt++)
    {
        j.push_back(*itPt);
    }
}

void from_json(const json& j, std::list<ST_CHANGE_REPORT_DATAPT>& lstReportPts)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_CHANGE_REPORT_DATAPT stReportPt = *js;
        lstReportPts.push_back(stReportPt);
    }
}

void to_json(json& j, const ST_DATACENTER_CHANGE_REPORT& stDataChangeReport)
{
    j = json{
        { "token", stDataChangeReport.token },
        { "timestamp", stDataChangeReport.timestamp },
        { "datatype",stDataChangeReport.datatype},
        { "body", stDataChangeReport.lstReportPts }
    };
}

void from_json(const json& j, ST_DATACENTER_CHANGE_REPORT& stDataChangeReport)
{
    json_to_obj<std::string>(j, "token", stDataChangeReport.token);
    json_to_obj<std::string>(j, "timestamp", stDataChangeReport.timestamp);
    json_to_obj<std::string>(j, "datatype", stDataChangeReport.datatype);
    json_to_obj<std::list<ST_CHANGE_REPORT_DATAPT>>(j, "body", stDataChangeReport.lstReportPts);
}

void to_json(json& j, const ST_REALTIME_QUERY_DEV& stRealtimeQueryDev)
{
    j = json{
        { "dev", stRealtimeQueryDev.dev },
        {"totalcall",stRealtimeQueryDev.totalcall},
        {"body",stRealtimeQueryDev.lstItemNames}
    };
}

void from_json(const json& j, ST_REALTIME_QUERY_DEV& stRealtimeQueryDev)
{
    json_to_obj<std::string>(j, "dev", stRealtimeQueryDev.dev);
    json_to_obj<std::string>(j, "totalcall", stRealtimeQueryDev.totalcall);
    json_to_obj<std::list<std::string>>(j, "body", stRealtimeQueryDev.lstItemNames);
}

void to_json(json& j, const ST_REALTIME_QUERY_REQ& stRealtimeQueryReq)
{
    j = json{
        { "token", stRealtimeQueryReq.token },
        { "timestamp", stRealtimeQueryReq.timestamp },
        {"body",stRealtimeQueryReq.lstRealtimeQueryDev}
    };
}

void from_json(const json& j, ST_REALTIME_QUERY_REQ& stRealtimeQueryReq)
{
    json_to_obj<std::string>(j, "token", stRealtimeQueryReq.token);
    json_to_obj<std::string>(j, "timestamp", stRealtimeQueryReq.timestamp);
    json_to_obj<std::list<ST_REALTIME_QUERY_DEV>>(j, "body", stRealtimeQueryReq.lstRealtimeQueryDev);
}

void to_json(json& j, const ST_REALTIME_QUERY_DATAITEM& stRealtimeQueryData)
{
    j = json{
        { "name", stRealtimeQueryData.name },
        { "val", stRealtimeQueryData.val },
        { "quality", stRealtimeQueryData.quality },
        { "timestamp", stRealtimeQueryData.timestamp }
    };
}

void from_json(const json& j, ST_REALTIME_QUERY_DATAITEM& stRealtimeQueryData)
{
    json_to_obj<std::string>(j, "name", stRealtimeQueryData.name);
    json_to_obj<std::string>(j, "val", stRealtimeQueryData.val);
    json_to_obj<std::string>(j, "quality", stRealtimeQueryData.quality);
    json_to_obj<std::string>(j, "timestamp", stRealtimeQueryData.timestamp);
}

void to_json(json& j, const ST_REALTIME_QUERY_DEV_RSP& stRealtimeQueryDevs)
{
    j = json{
        { "dev", stRealtimeQueryDevs.dev},
        {"body",stRealtimeQueryDevs.lstQueryDataItems}
    };
}

void from_json(const json& j, ST_REALTIME_QUERY_DEV_RSP& stRealtimeQueryDevs)
{
    json_to_obj<std::string>(j, "dev", stRealtimeQueryDevs.dev);
    json_to_obj<std::list<ST_REALTIME_QUERY_DATAITEM>>(j, "body", stRealtimeQueryDevs.lstQueryDataItems);
}

void to_json(json& j, const ST_REALTIME_QUERY_RSP& stRealtimeQueryRsp)
{
    j = json{
        { "token", stRealtimeQueryRsp.token },
        { "timestamp", stRealtimeQueryRsp.timestamp },
        {"body",stRealtimeQueryRsp.lstQueryDevsRsp}
    };
}

void from_json(const json& j, ST_REALTIME_QUERY_RSP& stRealtimeQueryRsp)
{
    json_to_obj<std::string>(j, "token", stRealtimeQueryRsp.token);
    json_to_obj<std::string>(j, "timestamp", stRealtimeQueryRsp.timestamp);
    json_to_obj<std::list<ST_REALTIME_QUERY_DEV_RSP>>(j, "body", stRealtimeQueryRsp.lstQueryDevsRsp);
}

void to_json(json& j, const ST_REMOTECTRL_CMD& stRemoteCtrlCmd)
{
    j = json{
        { "dev", stRemoteCtrlCmd.dev },
        { "name", stRemoteCtrlCmd.name },
        { "type", stRemoteCtrlCmd.type },
        { "cmd", stRemoteCtrlCmd.cmd },
        { "action", stRemoteCtrlCmd.action},
        { "model", stRemoteCtrlCmd.model},
        { "timeout", stRemoteCtrlCmd.timeout}
    };
}

void from_json(const json& j, ST_REMOTECTRL_CMD& stRemoteCtrlCmd)
{
    json_to_obj<std::string>(j, "dev", stRemoteCtrlCmd.dev);
    json_to_obj<std::string>(j, "name", stRemoteCtrlCmd.name);
    json_to_obj<std::string>(j, "type", stRemoteCtrlCmd.type);
    json_to_obj<std::string>(j, "cmd", stRemoteCtrlCmd.cmd);
    json_to_obj<std::string>(j, "action", stRemoteCtrlCmd.action);
    json_to_obj<std::string>(j, "model", stRemoteCtrlCmd.model);
    json_to_obj<std::string>(j, "timeout", stRemoteCtrlCmd.timeout);
}

void to_json(json& j, const std::list<ST_REMOTECTRL_CMD>& lstRemoteCtrlCmd)
{
    JSON_RETURN_ARRAY_EMPTY(lstRemoteCtrlCmd);
    for (auto itcmd = lstRemoteCtrlCmd.begin(); itcmd != lstRemoteCtrlCmd.end(); itcmd++)
    {
        j.push_back(*itcmd);
    }
}

void from_json(const json& j, std::list<ST_REMOTECTRL_CMD>& lstRemoteCtrlCmd)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_REMOTECTRL_CMD stRemoteCmd = *js;
        lstRemoteCtrlCmd.push_back(stRemoteCmd);
    }
}

void to_json(json& j, const ST_TRANS_REMOTE_CTRL& stRemoteCtrl)
{
    j = json{
        { "token", stRemoteCtrl.token },
        { "timestamp", stRemoteCtrl.timestamp },
        { "body", stRemoteCtrl.lstRemotrCtrl }
    };
}

void from_json(const json& j, ST_TRANS_REMOTE_CTRL& stRemoteCtrl)
{
    json_to_obj<std::string>(j, "token", stRemoteCtrl.token);
    json_to_obj<std::string>(j, "timestamp", stRemoteCtrl.timestamp);
    json_to_obj<std::list<ST_REMOTECTRL_CMD>>(j, "body", stRemoteCtrl.lstRemotrCtrl);
}

void  to_json(json& j, const ST_REMOTECTRL_RSP& stRemoteCtrlRsp)
{
    j = json{
        { "dev", stRemoteCtrlRsp.dev },
        { "name", stRemoteCtrlRsp.name },
        { "type", stRemoteCtrlRsp.type },
        { "cmd", stRemoteCtrlRsp.cmd },
        { "action", stRemoteCtrlRsp.action },
        { "status", stRemoteCtrlRsp.status }
    };
}

void from_json(const json& j, ST_REMOTECTRL_RSP& stRemoteCtrlRsp)
{
    json_to_obj<std::string>(j, "dev", stRemoteCtrlRsp.dev);
    json_to_obj<std::string>(j, "name", stRemoteCtrlRsp.name);
    json_to_obj<std::string>(j, "type", stRemoteCtrlRsp.type);
    json_to_obj<std::string>(j, "cmd", stRemoteCtrlRsp.cmd);
    json_to_obj<std::string>(j, "action", stRemoteCtrlRsp.action);
    json_to_obj<std::string>(j, "status", stRemoteCtrlRsp.status);
}

void to_json(json& j, const std::list<ST_REMOTECTRL_RSP>& lstRemoteCtrlRsp)
{
    JSON_RETURN_ARRAY_EMPTY(lstRemoteCtrlRsp);
    for (auto itrsp = lstRemoteCtrlRsp.begin(); itrsp != lstRemoteCtrlRsp.end(); itrsp++)
    {
        j.push_back(*itrsp);
    }
}

void from_json(const json& j, std::list<ST_REMOTECTRL_RSP>& lstRemoteCtrlRsp)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_REMOTECTRL_RSP stRemoteRsp = *js;
        lstRemoteCtrlRsp.push_back(stRemoteRsp);
    }
}

void to_json(json& j, const ST_TRANS_REMOTECTRL_RSP& stRemoteCtrlRsp)
{
    j = json{
        { "token", stRemoteCtrlRsp.token },
        { "timestamp", stRemoteCtrlRsp.timestamp },
        { "body", stRemoteCtrlRsp.lstRemotrCtrlRsp }
    };
}

void from_json(const json& j, ST_TRANS_REMOTECTRL_RSP& stRemoteCtrlRsp)
{
    json_to_obj<std::string>(j, "token", stRemoteCtrlRsp.token);
    json_to_obj<std::string>(j, "timestamp", stRemoteCtrlRsp.timestamp);
    json_to_obj<std::list<ST_REMOTECTRL_RSP>>(j, "body", stRemoteCtrlRsp.lstRemotrCtrlRsp);
}

//--------------------------------冻结数据-------------------------------------------------------
void  to_json(json& j, const ST_FROZEN_RPT_DATAPT stReportDataPt)
{
    j = {
        { "name", stReportDataPt.name },
        { "val", stReportDataPt.val }
    };
}

void from_json(const json& j, ST_FROZEN_RPT_DATAPT& stReportDataPt)
{
    json_to_obj<std::string>(j, "name", stReportDataPt.name);
    json_to_obj<std::string>(j, "val", stReportDataPt.val);
}

void to_json(json& j, const std::list<ST_FROZEN_RPT_DATAPT>& lstFrozenPts)
{
    JSON_RETURN_ARRAY_EMPTY(lstFrozenPts);
    for (auto itpt = lstFrozenPts.begin(); itpt != lstFrozenPts.end(); itpt++)
    {
        j.push_back(*itpt);
    }
}

void from_json(const json& j, std::list<ST_FROZEN_RPT_DATAPT>& lstFrozenPts)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_FROZEN_RPT_DATAPT stFrozenPt = *js;
        lstFrozenPts.push_back(stFrozenPt);
    }
}

void  to_json(json& j, const ST_FROZEN_GROUP_DATA stFrozenGroup)
{
    j = {
        { "timestamp", stFrozenGroup.timestamp },
        { "timestartgather", stFrozenGroup.timestartgather },
        { "timeendgather", stFrozenGroup.timeendgather },
        { "additionalcheck", stFrozenGroup.additionalcheck },
        { "body", stFrozenGroup.lstFrozenPts }
    };
}

void from_json(const json& j, ST_FROZEN_GROUP_DATA& stFrozenGroup)
{
    json_to_obj<std::string>(j, "timestamp", stFrozenGroup.timestamp);
    json_to_obj<std::string>(j, "timestartgather", stFrozenGroup.timestartgather);
    json_to_obj<std::string>(j, "timeendgather", stFrozenGroup.timeendgather);
    json_to_obj<std::string>(j, "additionalcheck", stFrozenGroup.additionalcheck);
    json_to_obj<std::list<ST_FROZEN_RPT_DATAPT>>(j, "body", stFrozenGroup.lstFrozenPts);
}

void  to_json(json& j, const std::list<ST_FROZEN_GROUP_DATA> lstFrozenGroups)
{
    JSON_RETURN_ARRAY_EMPTY(lstFrozenGroups);
    for (auto itgrp = lstFrozenGroups.begin(); itgrp != lstFrozenGroups.end(); itgrp++)
    {
        j.push_back(*itgrp);
    }
}

void from_json(const json& j, std::list<ST_FROZEN_GROUP_DATA>& lstFrozenGroups)
{
    for (auto js = j.begin(); js != j.end(); js++)
    {
        ST_FROZEN_GROUP_DATA stFrozenGrp = *js;
        lstFrozenGroups.push_back(stFrozenGrp);
    }
}

void  to_json(json& j, const ST_DATACENTER_FROZEN_DATAREPORT stFrozenReport)
{
    j = {
        { "token", stFrozenReport.token },
        { "timestamp", stFrozenReport.timestamp },
        { "data_row", stFrozenReport.data_row },
        { "frozentype", stFrozenReport.frozentype },
        { "body", stFrozenReport.lstReportPts }
    };
}

void from_json(const json& j, ST_DATACENTER_FROZEN_DATAREPORT stFrozenReport)
{
    json_to_obj<std::string>(j, "token", stFrozenReport.token);
    json_to_obj<std::string>(j, "timestamp", stFrozenReport.timestamp);
    json_to_obj<std::string>(j, "data_row", stFrozenReport.data_row);
    json_to_obj<std::string>(j, "frozentype", stFrozenReport.frozentype);
    json_to_obj<std::list<ST_FROZEN_GROUP_DATA>>(j, "body", stFrozenReport.lstReportPts);
}

void  to_json(json& j, const NOTIFY_SET_VALUE_BODY& obj)
{
    j = {
        { "dev", obj.dev },
        { "body", obj.body }
    };
}

void from_json(const json& j, NOTIFY_SET_VALUE_BODY& obj)
{
    json_to_obj<std::string>(j, "dev", obj.dev);
    json_to_obj<std::list<std::string>>(j, "body", obj.body);
}


void  to_json(json& j, const std::list<NOTIFY_SET_VALUE_BODY>& objs){
    JSON_RETURN_ARRAY_EMPTY(objs);
    for (auto obj = objs.begin(); obj != objs.end(); obj++){
        j.push_back(*obj);
    }
}

void from_json(const json& j, std::list<NOTIFY_SET_VALUE_BODY>& objs){
    for (auto js = j.begin(); js != j.end(); js++){
        NOTIFY_SET_VALUE_BODY obj = *js;
        objs.push_back(obj);
    }
}

void  to_json(json& j, const NOTIFY_SET_VALUE& obj)
{
    j = {
        { "token", obj.token },
        { "timestamp", obj.timestamp},
        { "body", obj.body }
    };
}

void from_json(const json& j, NOTIFY_SET_VALUE& obj)
{
    json_to_obj<std::string>(j, "token", obj.token);
    json_to_obj<std::string>(j, "timestamp", obj.timestamp);
    json_to_obj<std::list<NOTIFY_SET_VALUE_BODY>>(j, "body", obj.body);
}
void  to_json(json& j, const QUERY_SET_VALUE_BODY& obj)
{
    j = {
        { "dev", obj.dev },
        { "totalcall", obj.totalcall },
        { "body", obj.body }
    };
}

void from_json(const json& j, QUERY_SET_VALUE_BODY& obj)
{
    json_to_obj<std::string>(j, "dev", obj.dev);
    json_to_obj<std::string>(j, "totalcall", obj.totalcall);
    json_to_obj<std::list<std::string>>(j, "body", obj.body);
}


void  to_json(json& j, const std::list<QUERY_SET_VALUE_BODY>&& objs){
    JSON_RETURN_ARRAY_EMPTY(objs);
    for (auto obj = objs.begin(); obj != objs.end(); obj++){
        j.push_back(*obj);
    }
}

void from_json(const json& j, std::list<QUERY_SET_VALUE_BODY>& objs){
    for (auto js = j.begin(); js != j.end(); js++){
        QUERY_SET_VALUE_BODY obj = *js;
        objs.push_back(obj);
    }
}

void  to_json(json& j, const QUERY_SET_VALUE& obj)
{
    j = {
        { "token", obj.token },
        { "timestamp", obj.timestamp},
        { "body", obj.body }
    };
}

void from_json(const json& j, QUERY_SET_VALUE& obj){
    json_to_obj<std::string>(j, "token", obj.token);
    json_to_obj<std::string>(j, "timestamp", obj.timestamp);
    json_to_obj<std::list<QUERY_SET_VALUE_BODY>>(j, "body", obj.body);
}
//查询定值应答
void  to_json(json& j, const QUERY_SET_VALUE_DATAS_BODY_DATA& obj){
    j = {
        { "name", obj.name },
        { "val", obj.val},
        { "datatype", obj.datatype }
    };
}

void from_json(const json& j, QUERY_SET_VALUE_DATAS_BODY_DATA& obj){
    json_to_obj<std::string>(j, "name", obj.name);
    json_to_obj<std::string>(j, "val", obj.val);
    json_to_obj<std::string>(j, "datatype", obj.datatype);
}

void  to_json(json& j, const std::list<QUERY_SET_VALUE_DATAS_BODY_DATA>& objs){
    JSON_RETURN_ARRAY_EMPTY(objs);
    for (auto obj = objs.begin(); obj != objs.end(); obj++){
        j.push_back(*obj);
    }
}

void from_json(const json& j, std::list<QUERY_SET_VALUE_DATAS_BODY_DATA>& objs){
    for (auto js = j.begin(); js != j.end(); js++){
        QUERY_SET_VALUE_DATAS_BODY_DATA obj = *js;
        objs.push_back(obj);
    }
}

void  to_json(json& j, const QUERY_SET_VALUE_DATAS_BODY& obj)
{
    j = {
        { "dev", obj.dev },
        { "body", obj.body }
    };
}

void from_json(const json& j, QUERY_SET_VALUE_DATAS_BODY& obj)
{
    json_to_obj<std::string>(j, "dev", obj.dev);
    json_to_obj<std::list<QUERY_SET_VALUE_DATAS_BODY_DATA>>(j, "body", obj.body);
}


void  to_json(json& j, const std::list<QUERY_SET_VALUE_DATAS_BODY>& objs){
    JSON_RETURN_ARRAY_EMPTY(objs);
    for (auto obj = objs.begin(); obj != objs.end(); obj++){
        j.push_back(*obj);
    }
}

void from_json(const json& j, std::list<QUERY_SET_VALUE_DATAS_BODY>& objs){
    for (auto js = j.begin(); js != j.end(); js++){
        QUERY_SET_VALUE_DATAS_BODY obj = *js;
        objs.push_back(obj);
    }
}

void  to_json(json& j, const QUERY_SET_VALUE_DATAS& obj)
{
    j = {
        { "token", obj.token },
        { "timestamp", obj.timestamp},
        { "body", obj.body }
    };
}

void from_json(const json& j, QUERY_SET_VALUE_DATAS& obj){
    json_to_obj<std::string>(j, "token", obj.token);
    json_to_obj<std::string>(j, "timestamp", obj.timestamp);
    json_to_obj<std::list<QUERY_SET_VALUE_DATAS_BODY>>(j, "body", obj.body);
}

DcPacketManager::DcPacketManager()
{

}

DcPacketManager::~DcPacketManager()
{

}

int DcPacketManager::get_dev_register_msg(int64_t req_id, const std::list<ST_MODEL_INFO>& lstDevinfo, std::string &msg)
{
    ST_DEV_REGISTER stDevRegister;

    stDevRegister.token = std::to_string(req_id);
    get_uart_timestamp(stDevRegister.timestamp);
    for (auto itMode = lstDevinfo.begin(); itMode != lstDevinfo.end(); itMode++)
    {
        stDevRegister.lstModelInfo.push_back(*itMode);
    }

    json j = stDevRegister;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_dev_unreg_msg(int64_t req_id, const std::list<ST_UNREG_DEVINFO>& lstDevinfo, std::string &msg)
{
    ST_DEV_UNREGISTER stUnDevReg;

    stUnDevReg.token = std::to_string(req_id);
    get_uart_timestamp(stUnDevReg.timestamp);
    for (auto itMode = lstDevinfo.begin(); itMode != lstDevinfo.end(); itMode++)
    {
        stUnDevReg.lstDevInfo.push_back(*itMode);
    }

    json j = stUnDevReg;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_dev_register_notify(const std::string& msg, std::list<ST_REGISTER_NOTIFYPT>& lstRegisterNotify)
{
    if (msg.empty())
    {
        return -1;
    }

    auto j = json::parse(msg);
    if (!j.contains("body")) {
        CLogger::getInstance()->log_d("get_dev_register_notify json data error.");
        return -1;
    }

    ST_DEV_REGISTER_NOTIFY stModelRegNotify = j;
    CLogger::getInstance()->log_d("register Notify, token=%s,timestamp=%s,notify num=%d",stModelRegNotify.token.c_str(),stModelRegNotify.timestamp.c_str(),stModelRegNotify.lstRegisterPts.size());

    for (auto itNotify = stModelRegNotify.lstRegisterPts.begin(); itNotify != stModelRegNotify.lstRegisterPts.end(); itNotify++)
    {
        lstRegisterNotify.push_back(*itNotify);
    }

    return lstRegisterNotify.size();
}

int DcPacketManager::get_dc_comm_rsp(const std::string& msg, std::string& status)
{
    if (msg.empty()) {
        return -1;
    }

    auto j = json::parse(msg);
    //if (nullptr == j["body"]) {
    //    return -1;
    //}
    status == j["status"].get<std::string>();

    return status.length();
}

int DcPacketManager::get_model_set_msg(int64_t req_id, const std::string& strmodel, std::list<ST_MODEL_PTINFO>& lstModels, std::string& msg)
{
    ST_MODEL_REGISTER  stModelReg;

    stModelReg.token = std::to_string(req_id);
    stModelReg.model = strmodel;
    get_uart_timestamp(stModelReg.timestamp);

    for (auto itm = lstModels.begin(); itm != lstModels.end(); itm++)
    {
        stModelReg.lstModelInfo.push_back(*itm);
    }

    json j = stModelReg;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_model_dele_msg(int64_t req_id, const std::list<std::string>& lstModels, std::string& msg)
{
    ST_MODEL_UNREGISTER  stModelUnreg;

    stModelUnreg.token = std::to_string(req_id);
    get_uart_timestamp(stModelUnreg.timestamp);

    for (auto itm = lstModels.begin(); itm != lstModels.end();itm++)
    {
        stModelUnreg.lstmodels.push_back(*itm);
    }

    json j = stModelUnreg;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_datareport_msg(int64_t req_id, std::list<ST_REPORT_DATAPT>& lstReportPts, std::string& msg)
{
    ST_DATACENTER_DATAREPORT  stDataReport;

    stDataReport.token = std::to_string(req_id);
    stDataReport.data_row = "single";
    get_uart_timestamp(stDataReport.timestamp);

    for (auto itRpt = lstReportPts.begin(); itRpt != lstReportPts.end(); itRpt++)
    {
        stDataReport.lstReportPts.push_back(*itRpt);
    }

    json j = stDataReport;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_model_query_request(int64_t req_id, const std::list<std::string>& lstModelNames, std::string& msg)
{
    ST_MODEL_QUERY  stModelQuery;

    stModelQuery.token = std::to_string(req_id);
    get_uart_timestamp(stModelQuery.timestamp);
    for (auto itname = lstModelNames.begin(); itname != lstModelNames.end(); itname++)
    {
        stModelQuery.lstModelNames.push_back(*itname);
    }

    json j = stModelQuery;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_model_query_response(const std::string& msg, std::list<ST_SINGLE_MODEL_INFO>& lstModels)
{
    if (msg.empty())
    {
        return -1;
    }

    auto j = json::parse(msg);
    if (!j.contains("body")) {
        CLogger::getInstance()->log_d("get_model_query_response json data error.");
        return -1;
    }

    ST_MODEL_QUERY_RSP  stModelQuery = j;

    for (auto itm = stModelQuery.lstModels.begin(); itm != stModelQuery.lstModels.end(); itm++)
    {
        ST_SINGLE_MODEL_INFO  stModelInfo;
        stModelInfo.model = itm->model;
        for (auto itInfo = itm->lstModelptInfo.begin(); itInfo != itm->lstModelptInfo.end(); itInfo++)
        {
            stModelInfo.lstModelptInfo.push_back(*itInfo);
        }
        lstModels.push_back(stModelInfo);
    }

    return lstModels.size();
}

int DcPacketManager::get_model_name_query_rsp(const std::string& msg, std::list<std::string>& lstModelNames)
{
    if (msg.empty())
    {
        CLogger::getInstance()->log_d("get_model_name_query_rsp json is null.");
        return -1;
    }

    auto j = json::parse(msg);
    if (!j.contains("body")) {
        CLogger::getInstance()->log_d("get_model_name_query_rsp json body is null.");
        return -1;
    }

    ST_MODEL_QUERY  stModelNames = j;
    for (auto itm = stModelNames.lstModelNames.begin(); itm != stModelNames.lstModelNames.end(); itm++)
    {
        lstModelNames.push_back(*itm);
    }

    return lstModelNames.size();
}

int DcPacketManager::get_datachange_report_msg(int64_t req_id, int datatype, std::list<ST_CHANGE_REPORT_DATAPT>& lstReportPts, std::string& msg)
{
    ST_DATACENTER_CHANGE_REPORT  stDataChangeReport;

    stDataChangeReport.token = std::to_string(req_id);
    stDataChangeReport.datatype = std::to_string(datatype);
    get_uart_timestamp(stDataChangeReport.timestamp);

    for (auto itpt = lstReportPts.begin(); itpt != lstReportPts.end(); itpt++)
    {
        stDataChangeReport.lstReportPts.push_back(*itpt);
    }

    json j = stDataChangeReport;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_model_register_relationship_req(int64_t req_id, std::string& msg)
{
    ST_DEV_REGISTER_RELATIONSHIP  stRegisterRelation;

    stRegisterRelation.token = std::to_string(req_id);
    get_uart_timestamp(stRegisterRelation.timestamp);

    json j = stRegisterRelation;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_model_register_relationship_rsp(const std::string& msg, std::list<ST_REGISTER_MODEL_INFO>& lstRegisterRelation)
{
    if (msg.empty())
    {
        return -1;
    }

    auto j = json::parse(msg);
    if (!j.contains("body")) {
        CLogger::getInstance()->log_d("get_model_register_relationship_rsp json data is null.");
        return -1;
    }

    ST_DEV_REGISTER_RELATIONSHIP  stRegisterRelation = j;
    for (auto itReg = stRegisterRelation.lstRegisterModels.begin(); itReg != stRegisterRelation.lstRegisterModels.end(); itReg++)
    {
        ST_REGISTER_MODEL_INFO stRegisterModel = *itReg;
        lstRegisterRelation.push_back(stRegisterModel);
    }

    return lstRegisterRelation.size();
}

int DcPacketManager::get_trans_remotectrl_req_msg(int64_t req_id, std::list<ST_REMOTECTRL_CMD>& lstRemotrCtrl, std::string& msg)
{
    ST_TRANS_REMOTE_CTRL  stTransRemoteCtrl;

    stTransRemoteCtrl.token = std::to_string(req_id);
    get_uart_timestamp(stTransRemoteCtrl.timestamp);

    for (auto itCmd = lstRemotrCtrl.begin(); itCmd != lstRemotrCtrl.end(); itCmd++)
    {
        stTransRemoteCtrl.lstRemotrCtrl.push_back(*itCmd);
    }

    json j = stTransRemoteCtrl;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_trans_remotectrl_rsp_data(const std::string& msg, std::list<ST_REMOTECTRL_RSP>& lstRemotrCtrlRsp)
{
    if (msg.empty())
    {
        CLogger::getInstance()->log_d("get_trans_remotectrl_rsp_data msg is null.");
        return -1;
    }

    auto j = json::parse(msg);
    if (!j.contains("body")) {
        CLogger::getInstance()->log_d("get_trans_remotectrl_rsp_data json body is null.");
        return -1;
    }

    ST_TRANS_REMOTECTRL_RSP  stTransRemoteCtrlRsp =j;
    
    for (auto itCmd = stTransRemoteCtrlRsp.lstRemotrCtrlRsp.begin(); itCmd != stTransRemoteCtrlRsp.lstRemotrCtrlRsp.end(); itCmd++)
    {
        lstRemotrCtrlRsp.push_back(*itCmd);
    }
    
    return lstRemotrCtrlRsp.size();
}

int  DcPacketManager::get_realtime_data_req_msg(int64_t req_id, const std::list<std::string>& lstDevs, std::string& msg)
{
    ST_REALTIME_QUERY_REQ stRealtimeQueryReq;

    stRealtimeQueryReq.token = std::to_string(req_id);
    get_uart_timestamp(stRealtimeQueryReq.timestamp);
    for (auto itd = lstDevs.begin(); itd != lstDevs.end(); itd++)
    {
        ST_REALTIME_QUERY_DEV  stQueryDev;
        stQueryDev.dev = *itd;
        stQueryDev.totalcall = "1";
        stRealtimeQueryReq.lstRealtimeQueryDev.push_back(stQueryDev);
    }

    json j = stRealtimeQueryReq;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_realtime_data_rsp_data(const std::string& msg, std::list<ST_REALTIME_QUERY_DEV_RSP>& lstRealtimeQueryRsp)
{
    if (msg.empty())
    {
        CLogger::getInstance()->log_d("get_realtime_data_rsp_data msg is null.");
        return -1;
    }

    auto j = json::parse(msg);
    if (!j.contains("body")) {
        CLogger::getInstance()->log_d("get_realtime_data_rsp_data json body is null.");
        return -1;
    }

    ST_REALTIME_QUERY_RSP  stRealtimeQueryRsp = j;
    for (auto itd = stRealtimeQueryRsp.lstQueryDevsRsp.begin(); itd != stRealtimeQueryRsp.lstQueryDevsRsp.end(); itd++)
    {
        ST_REALTIME_QUERY_DEV_RSP  stRealtimeDev;
        stRealtimeDev.dev = itd->dev;
        for (auto itr = itd->lstQueryDataItems.begin(); itr != itd->lstQueryDataItems.end(); itr++)
        {
            stRealtimeDev.lstQueryDataItems.push_back(*itr);
        }
        lstRealtimeQueryRsp.push_back(*itd);
    }

    return lstRealtimeQueryRsp.size();
}

int DcPacketManager::get_frozen_datareport_msg(int64_t req_id, std::list<ST_FROZEN_GROUP_DATA>& lstfrozen_group, std::string frozen_type, std::string& msg)
{
    ST_DATACENTER_FROZEN_DATAREPORT  stDcFrozenReport;

    stDcFrozenReport.token = std::to_string(req_id);
    stDcFrozenReport.data_row = "frozen";
    get_uart_timestamp(stDcFrozenReport.timestamp);

    stDcFrozenReport.frozentype = frozen_type;
   
    for (auto itg = lstfrozen_group.begin(); itg != lstfrozen_group.end(); itg++)
    {
        stDcFrozenReport.lstReportPts.push_back(*itg);
    }

    json j = stDcFrozenReport;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_notify_set_value_msg(NOTIFY_SET_VALUE& notify, std::string &msg)
{
    if (msg.empty()){
        return -1;
    }
    
    json j = json::parse(msg);
    
    notify = j;

    return msg.length();
}

int DcPacketManager::get_query_set_value_msg(QUERY_SET_VALUE& query, std::string &msg)
{
    get_uart_timestamp(query.timestamp);
    json j = query;
    msg = j.dump();

    return msg.length();
}

int DcPacketManager::get_query_set_value_obj(QUERY_SET_VALUE_DATAS& query, std::string &msg)
{
    if (msg.empty()){
        return -1;
    }

    auto j = json::parse(msg);
    query = j;

    return msg.length();
}

